#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;

namespace cls
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}

		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_size+1];
			strcpy(_str,str);
		}

		/*string(const string& s)
		{
			_size = s._size;
			_capacity = s._capacity;
			_str = new char[_capacity + 1];
			strcpy(_str,s._str);
		}*/

		string(const string& s)
		{
			string temp(s._str);
			swap(temp);
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}

		size_t size() const
		{
			return _size;
		}

		size_t capacity() const
		{
			return _capacity;
		}
		
		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size);
			return _str[pos];
		}


		void clear()
		{
			_size = 0;
			_str[0] = '\0';
		}

		const char* c_str() const
		{
			return _str;
		}

		/*string& operator=(const string& s)
		{
			delete[] _str;
			_size = s._size;
			_capacity = s._capacity;
			_str = new char[1 + _capacity];
			strcpy(_str,s._str);

			return *this;
		}*/

		string& operator=(string s)
		{
			swap(s);
			   
			return *this;
		}

		void swap(string& str)
		{
			std::swap(_str, str._str);
			std::swap(_size,str._size);
			std::swap(_capacity,str._capacity);
		}

		void reserve(size_t n);

		void push_back(char ch);

		void append(const char* s);
	
		string& operator+=(char ch);

		string& operator+=(const  char* s);

		void insert(size_t pos,char ch);

		void insert(size_t pos,const char* s);

		void erase(size_t pos , size_t n = npos);

		size_t find(char ch,size_t pos = 0);

		size_t find(const char* s, size_t pos = 0);

		string substr(size_t pos = 0,size_t n = npos);

		bool operator<(const string& str);

		bool operator<=(const string& str);

		bool operator>(const string& str);

		bool operator>=(const string& str);

		bool operator==(const string& str);

		bool operator!=(const string& str);
		
		static const size_t npos;
	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;
	};
	
	ostream& operator<<(ostream& out, const string& str);

	istream& operator>>(istream& in, string& str);

}
